﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;

namespace RedisDBBiz
{
    public class RoomPriceGetRedis
    {
        public static int LeftTotalKeysCount = 0;
        public static int RightTotalKeysCount = 0;
        public static int SameKeyCount = 0;
        public static int SameKeyDifferentValueCount = 0;
        public static int DefKeyCount = 0;
        public static int LeftLostKeyCount = 0;
        public static int RightNewKeyCount = 0;
        public static int SameKeyDefPriceCount = 0;

        public IRedisClient redisClient;
        public RoomPriceGetRedis()
        {
            redisClient = CreateCacheClient();
        }

        public IRedisClient CreateCacheClient()
        {
            var client = new ServiceStack.Redis.BasicRedisClientManager("localhost").GetClient();
            return client;
        }

        public List<string> GetList(string key)
        {
            return redisClient.GetRangeFromList(key, 0, -1);
        }

        public void FlushDB()
        {
            redisClient.FlushDb();
        }



        public void DoCompare(List<int> roomList)
        {
            foreach(var room in roomList){
                List<string> pLeftKey = redisClient.SearchKeys(":" + room + ":*:P2");
                List<string> p2RightKey = redisClient.SearchKeys(":" + room + ":*:P3");
                LeftTotalKeysCount += pLeftKey.Count();
                RightTotalKeysCount += p2RightKey.Count();

                pLeftKey.Sort();
                p2RightKey.Sort();
                foreach (var keyInLeft in pLeftKey)
                {
                    foreach (var keyInRight in p2RightKey)
                    {
                        if (keyInRight.Substring(0, keyInRight.Length - 1).Equals(keyInLeft.Substring(0, keyInLeft.Length - 1)))
                        {
                            List<string> valueLeft = redisClient.GetRangeFromList(keyInLeft,0,-1);
                            List<string> valueRight = redisClient.GetRangeFromList(keyInRight, 0, -1);
                            if (valueLeft.First() == valueRight.First())
                            {
                                redisClient.AddItemToList("Summary:SameKeyP23", keyInLeft);
                                SameKeyCount++;
                            }
                            else
                            {
                                SameKeyDifferentValueCount++;
                            }
                        }
                    }
                }
            }

            redisClient.Add("Summary:LeftCountP23", LeftTotalKeysCount);
            redisClient.Add("Summary:RightCountP23", RightTotalKeysCount);
            redisClient.Add("Summary:SameKeyP23", SameKeyCount);
            redisClient.Add("Summary:SameKeyDifValP23", SameKeyDifferentValueCount);
            
        }

        public void SetList(List<string> hotelList)
        {
            redisClient.AddRangeToList("BookingHotelCode", hotelList);
        }

        public void AddItemToList(string key ,string item)
        {
            redisClient.AddItemToList(key, item);
        }

        public void SetItema(string key, string value)
        {
            redisClient.Add(key, value);
        }
    }
}
